package com.cskaoyan.service;

import com.cskaoyan.bean.MarketComment;
import com.cskaoyan.bean.MarketCommentExample;
import com.cskaoyan.bean.MarketUser;
import com.cskaoyan.bean.brand.UserInfo;
import com.cskaoyan.bean.comment.bo.ListCommentWxBo;
import com.cskaoyan.bean.comment.vo.CommentWxListVo;
import com.cskaoyan.bean.comment.vo.CountVo;
import com.cskaoyan.bean.common.CommonData;
import com.cskaoyan.bean.goods.bo.BasePageInfoBo;
import com.cskaoyan.mapper.MarketCommentMapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.PrincipalCollection;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class CommentServiceImpl implements CommentService {

    @Autowired
    MarketCommentMapper marketCommentMapper;

    @Override
    public CommonData<MarketComment> queryAllComments(BasePageInfoBo basePageInfoBo) {
        //分页设置
        PageHelper.startPage(basePageInfoBo.getPage(), basePageInfoBo.getLimit());
        MarketCommentExample example = new MarketCommentExample();
        example.setOrderByClause(basePageInfoBo.getSort() + " " + basePageInfoBo.getOrder());
        MarketCommentExample.Criteria criteria = example.createCriteria();
        // 查找
        criteria.andDeletedEqualTo(false);//已经删除的不显示

        if (basePageInfoBo.getUserId() != null && !"".equals(basePageInfoBo.getUserId())) {//StringUtils isEmpty
            criteria.andUserIdEqualTo(basePageInfoBo.getUserId());
        }

        if (basePageInfoBo.getValueId() != null && !"".equals(basePageInfoBo.getValueId())) {
            criteria.andValueIdEqualTo(basePageInfoBo.getValueId());
        }

        //在数据库里面查询list
        List<MarketComment> marketCommentsList = marketCommentMapper.selectByExample(example);

        //给要返回的对象传入数据（list和4个数据），做手脚
        CommonData<MarketComment> marketGoodsCommonData = new CommonData<>();
        marketGoodsCommonData.setLimit(basePageInfoBo.getLimit());
        marketGoodsCommonData.setPage(basePageInfoBo.getPage());
        //这些是要查完数据库才有的数据
        PageInfo pageInfo = new PageInfo(marketCommentsList);
        Integer total = (int) pageInfo.getTotal();
        int pages = pageInfo.getPages();
        //继续赋值
        marketGoodsCommonData.setPages(pages);
        marketGoodsCommonData.setTotal(total);
        marketGoodsCommonData.setList(marketCommentsList);
        return marketGoodsCommonData;
    }

    @Override
    public int deleteComments(MarketComment marketComment) {
        MarketCommentExample example = new MarketCommentExample();
        MarketCommentExample.Criteria criteria = example.createCriteria();
        criteria.andIdEqualTo(marketComment.getId());

        //如果deleted已经不存在就不用了改了
        if (marketComment.getDeleted()) {
            return 1;
        }

        marketComment.setDeleted(true);
        int code = marketCommentMapper.updateByExampleSelective(marketComment, example);

        return code;
    }

    @Override
    public CommonData<CommentWxListVo> listAllCommentWx(ListCommentWxBo listCommentWxBo) {
        //分页设置
        PageHelper.startPage(listCommentWxBo.getPage(), listCommentWxBo.getLimit());
        MarketCommentExample example = new MarketCommentExample();
        MarketCommentExample.Criteria criteria = example.createCriteria();

        criteria.andValueIdEqualTo(listCommentWxBo.getValueId());
        criteria.andTypeEqualTo(listCommentWxBo.getType());
        example.setOrderByClause("update_time desc");

        List<MarketComment> marketCommentsList = null;

        if (listCommentWxBo.getShowType() == 1) {
            criteria.andHasPictureEqualTo(true);//有图
            //查数据库
            marketCommentsList = marketCommentMapper.selectByExample(example);
        } else if (listCommentWxBo.getShowType() == 0) {
            marketCommentsList = marketCommentMapper.selectByExample(example);//没有图
        }


        //给要返回的对象传入数据（list和4个数据），做手脚
        CommonData<CommentWxListVo> marketGoodsCommonData = new CommonData<>();
        marketGoodsCommonData.setLimit(listCommentWxBo.getLimit());
        marketGoodsCommonData.setPage(listCommentWxBo.getPage());
        //这些是要查完数据库才有的数据
        PageInfo pageInfo = new PageInfo(marketCommentsList);
        Integer total = (int) pageInfo.getTotal();
        int pages = pageInfo.getPages();
        //继续赋值
        marketGoodsCommonData.setPages(pages);
        marketGoodsCommonData.setTotal(total);
        //这里是变量List的值
        ArrayList<CommentWxListVo> objects = new ArrayList<>();
        //获取用户信息的
        Subject subject = SecurityUtils.getSubject();
        MarketUser primaryPrincipal = (MarketUser) subject.getPrincipals().getPrimaryPrincipal();
        String nickname = primaryPrincipal.getNickname();
        String avatar = primaryPrincipal.getAvatar();
        UserInfo userInfo1 = new UserInfo();
        userInfo1.setAvatarUrl(avatar);
        userInfo1.setNickName(nickname);
        for (MarketComment marketComment : marketCommentsList) {
            CommentWxListVo commentWxListVo = new CommentWxListVo();
            //commentWxListVo对象赋值
            commentWxListVo.setAddTime(marketComment.getAddTime());
            commentWxListVo.setAdminContent(marketComment.getAdminContent());
            commentWxListVo.setContent(marketComment.getContent());
            commentWxListVo.setPicList(marketComment.getPicUrls());
            commentWxListVo.setUserInfo(userInfo1);
            //赋值后加入集合中
            objects.add(commentWxListVo);
        }
        marketGoodsCommonData.setList(objects);

        return marketGoodsCommonData;

    }

    @Override
    public CountVo queryCommentCount(MarketComment marketComment) {
        MarketCommentExample example = new MarketCommentExample();
        //限制条件
        MarketCommentExample.Criteria criteria = example.createCriteria();
        criteria.andValueIdEqualTo(marketComment.getValueId());
        criteria.andTypeEqualTo(marketComment.getType());

        //查数据库
        List<MarketComment> marketCommentsList = marketCommentMapper.selectByExample(example);
        ArrayList<MarketComment> objects = new ArrayList<>();
        for (MarketComment comment : marketCommentsList) {
            if (comment.getHasPicture()) {
                objects.add(comment);
            }
        }

        int size = marketCommentsList.size();
        int size1 = objects.size();

        //赋值
        CountVo countVo = new CountVo();
        countVo.setAllCount(size);
        countVo.setHasPicCount(size1);
        return countVo;
    }

    @Override
    public void insertComment(MarketComment marketComment) {
        MarketCommentExample marketCommentExample = new MarketCommentExample();
        MarketCommentExample.Criteria criteria = marketCommentExample.createCriteria();

        //取出ID
        Subject subject = SecurityUtils.getSubject();
        MarketUser primaryPrincipal = (MarketUser) subject.getPrincipals().getPrimaryPrincipal();
        Integer uid = primaryPrincipal.getId();

        //设置条件去插入
        criteria.andUserIdEqualTo(uid);
        criteria.andValueIdEqualTo(marketComment.getValueId());
        criteria.andTypeEqualTo(marketComment.getType());
        marketCommentMapper.insertSelective(marketComment);
    }
}
